package com.ruoyi.service.controller;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.service.domain.TongJi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.service.domain.ServiceDetails;
import com.ruoyi.service.service.IServiceDetailsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 服务详情Controller
 * 
 * @author wsc
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/details")
public class ServiceDetailsController extends BaseController
{
    @Autowired
    private IServiceDetailsService serviceDetailsService;

    /**
     * 查询服务详情列表
     */
    @RequiresPermissions("evaluate:details:list")
    @GetMapping("/list")
    public TableDataInfo list(ServiceDetails serviceDetails)
    {
        startPage();
        List<ServiceDetails> list = serviceDetailsService.selectServiceDetailsList(serviceDetails);
        return getDataTable(list);
    }

    /**
     * 导出服务详情列表
     */
    @RequiresPermissions("evaluate:details:export")
    @Log(title = "服务详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ServiceDetails serviceDetails)
    {
        List<ServiceDetails> list = serviceDetailsService.selectServiceDetailsList(serviceDetails);
        ExcelUtil<ServiceDetails> util = new ExcelUtil<ServiceDetails>(ServiceDetails.class);
        util.exportExcel(response, list, "服务详情数据");
    }

    /**
     * 获取服务详情详细信息
     */
    @RequiresPermissions("evaluate:details:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(serviceDetailsService.selectServiceDetailsById(id));
    }

    /**
     * 新增服务详情
     */
    @RequiresPermissions("evaluate:details:add")
    @Log(title = "服务详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServiceDetails serviceDetails)
    {
        return toAjax(serviceDetailsService.insertServiceDetails(serviceDetails));
    }

    /**
     * 修改服务详情
     */
    @RequiresPermissions("evaluate:details:edit")
    @Log(title = "服务详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServiceDetails serviceDetails)
    {
        return toAjax(serviceDetailsService.updateServiceDetails(serviceDetails));
    }

    /**
     * 删除服务详情
     */
    @RequiresPermissions("evaluate:details:remove")
    @Log(title = "服务详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(serviceDetailsService.deleteServiceDetailsByIds(ids));
    }
    @GetMapping("/tongji/{id}")
    public AjaxResult tongji(@PathVariable("id") Long id) {
        List<TongJi> tongJis = serviceDetailsService.selectServiceDetailsCount(id);
        List<TongJi> list = new ArrayList<>();
        list.add(new TongJi("洗车", 0L));
        list.add(new TongJi("保养", 0L));
        list.add(new TongJi("维修", 0L));
           tongJis.forEach(tongJi -> {
            if ("专业洗车".equals(tongJi.getName())) {
                list.get(0).setData(tongJi.getData());
            }
            else if ("车辆保养".equals(tongJi.getName())) {
                list.get(1).setData(tongJi.getData());
            }
            else  if ("车辆维修".equals(tongJi.getName())) {
                list.get(2).setData(tongJi.getData());
            }
           });

        return success(list);
    }

//    @GetMapping("/tongji1/{id}")
//    public AjaxResult tongji1(@PathVariable("id") Long id) {
//        System.out.println("id:"+id);
//        List<HashMap<Integer, Object>> list = serviceDetailsService.selectServiceDetailsCountByMap(id);
//        HashMap<String, Object> map = new HashMap<>();
//System.out.println(list);
//        List list1 =  new ArrayList();
//        int i = 0;
//        if (list != null && !list.isEmpty()) {
//            //System.out.println(list);
//            for (HashMap<Integer, Object> hashMap : list) {
//                Integer key = 0;
//                String name = "";
//                Integer value = 0;
//                for (Map.Entry<Integer, Object> entry : hashMap.entrySet()) {
//                    if(i==0) {
//                        map = new HashMap<>();
//                    }
//                    i++;
//                    //System.out.println(entry.getKey());
//                    if ("name".equals(entry.getKey())) {
//                        key = (Integer) entry.getValue();
//                        if(key==1){
//                            name = "专业洗车";
//                        }else if(key==2){
//                            name = "车辆保养车";
//                        }else if(key==3) {
//                            name = "车辆维修";
//                        }
//                        map.put("name", name);
//                    } else if ("value".equals(entry.getKey())) {
////我需要的是int型所以做了如下转换，实际上返回的object应为Long。
//                        value =  ((Long)entry.getValue()).intValue();
//                        map.put("value", value);
//                    }
//                    if(i==2){
//                        list1.add(map);
//                        i=0;
//                    }
//                }
//            }
//            System.out.println(list1);
//        }
//        return  success(list1);
//    }

}
